#!/usr/bin/env python
#--coding:utf-8--
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from 主机管理.models import SQLClass
from 主机管理.models import loggingCls
import easygui
import json
servicest=loggingCls.logCls("../logs/services.log")
def showServices():
    '''
    直接查看所有主机和主机对应的组，选择主机，返回主机的ip地址
    :return:
    '''
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Hosts)
    service_list=[]
    for host in a.watch():
        service_list.append([host.hostname,[host.ipattr,host.port],host.Groups.groupname])

    host_ret=easygui.choicebox("主机列表",choices=service_list)
    if not host_ret:
        return 0
    else:

        host_ret = host_ret.replace('\'', '\"')
        print(host_ret)
        host_ret = json.loads(host_ret)
        host_ret[1][1] = int(host_ret[1][1])
        host_ret[1] = tuple(host_ret[1])
        return host_ret
def showGroups():
    '''
    查看所有组，选择其中一个组，列出这个组中所有的主机，选择主机返回ip地址
    :return:
    '''
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Groups)
    group_list=[]
    service_list=[]
    for group in a.watch():
        for host in group.Hosts:
            group_list.append([group.groupid,group.groupname,host.hostname])
    group_ret = easygui.choicebox("组列表",choices=group_list)
    if not group_ret:
        return
    else:
        group_ret=group_ret.replace('\'','\"')
        group_ret =json.loads(group_ret)
    a.addTable(SQLClass.Hosts)

    for host in a.watch(filter=SQLClass.Hosts.groupid==group_ret[0]):
        service_list.append([host.hostname,[host.ipattr,host.port],host.Groups.groupname])
    host_ret=easygui.choicebox("主机列表",choices=service_list)
    if not host_ret:
        return
    else:
        host_ret = host_ret.replace('\'', '\"')
        host_ret = json.loads(host_ret)
        host_ret[1][1]=int(host_ret[1][1])
        host_ret[1]=tuple(host_ret[1])
        return host_ret
def addservice():
    ret=easygui.multenterbox("添加主机",fields=("主机名","ip地址","端口号","组名"))
    if not ret:
        return
    else:
        hostname, ipattr,port, groupname=ret

    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Groups)
    find_groups=a.watch(filter=SQLClass.Groups.groupname==groupname)
    if not find_groups:
        flag=easygui.boolbox("组%s不存在，是否创建"%groupname,choices=("是","否"))
        if flag:
            a.add(groupname=groupname)
        else:
            return
        easygui.msgbox("恭喜成功创建组%s"%groupname)
        servicest.infile("创建了组%s"%groupname)
    else:
        easygui.msgbox("组%s已存在"%groupname)

    groupid=a.watch(filter=SQLClass.Groups.groupname == groupname)[0].groupid

    a.addTable(SQLClass.Hosts)
    find_hosts=a.watch(filter=SQLClass.Hosts.hostname == hostname)
    if not find_hosts:
        flag = easygui.boolbox("主机%s不存在，是否创建" % hostname, choices=("是", "否"))
        if flag:
            a.add(hostname=hostname,ipattr=ipattr,port=port,groupid=groupid)
        easygui.msgbox('''恭喜成功创建主机%s
        ip:%s
        ''' % (hostname,ipattr))
        servicest.infile("创建了主机%s" % hostname)
    else:
        easygui.msgbox("主机%s已存在" % hostname)
    a.commit()

def delservice():
    ret=easygui.multenterbox("删除主机",fields=("主机名", "ip地址"))
    if not ret:
        return
    else:
        hostname, ipattr=ret
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Hosts)
    find_hosts=a.watch(filter=SQLClass.Hosts.hostname == hostname)
    if not find_hosts:
        easygui.msgbox("主机不存在，无法删除")
        return 0
    a.delete(filter=SQLClass.Hosts.hostname == hostname)
    a.commit()
    easygui.msgbox("成功删除主机%s"%hostname)
    servicest.infile("删除了主机%s" % hostname)

def addgroup():
    ret = easygui.enterbox("请输入组名","添加组")
    if not ret:
        return
    else:
        groupname = ret
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Groups)
    find_groups = a.watch(filter=SQLClass.Groups.groupname == groupname)
    if not find_groups:
        flag = easygui.boolbox("组%s不存在，是否创建" % groupname, choices=("是", "否"))
        if flag:
            a.add(groupname=groupname)
            a.commit()
        else:
            return
        easygui.msgbox("恭喜成功创建组%s" % groupname)
        servicest.infile("成功创建组%s" % groupname)
    else:
        easygui.msgbox("组%s已存在" % groupname)


def delgroup():
    ret = easygui.enterbox("请输入组名", "添加组")
    if not ret:
        return
    else:
        groupname = ret
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Groups)
    find_groups = a.watch(filter=SQLClass.Groups.groupname == groupname)
    if not find_groups:
        easygui.msgbox("你输入的组名%s不存在"%groupname)
        return

    a.delete(filter=SQLClass.Groups.groupname == groupname)
    easygui.msgbox("成功删除了组%s"%groupname)
    servicest.infile("删除了组%s" % groupname)
    a.commit()
